package com.kgc.scd.consumer;

import com.kgc.scd.contant.SeckillConstant;
import com.kgc.scd.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * Created On : 2022/6/8.
 * <p>
 * Author     : 98730
 * <p>
 * Description: 生成订单消息队列监听
 */
@Slf4j
@Component
public class TradeOrderSaveListener {

    @Autowired
    private RedisUtils redisUtils;

    @JmsListener(destination = SeckillConstant.SECKILL_CREATE_ORDER_QUEUE_NAME)
    @SendTo(SeckillConstant.SECKILL_SECKILL_RESP_QUEUE_NAME)
    public String saveTradeOrderFromQueue(Map<String,String> tradeOrder){
        log.info("+++++++ 生成订单请求：{}，开始入库 ++++++",tradeOrder);

        // TODO 直接将队列中获取的订单实体，入库，同时改变redis中的订单状态为1（已创建）

        // 模拟订单状态的改变
        redisUtils.set(tradeOrder.get("tradeOrderNo"),1);

        log.info("+++++++ 生成订单请求：{}，入库成功 ++++++",tradeOrder);

        return String.format("生成订单请求：%s，入库成功", tradeOrder);
    }
}
